package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.view.TitleBar;

import java.io.File;

/**
 * Created by zw on 2016/3/7 0007.
 * 选择照片
 */
public class ChooseImageActivity extends ABPhotoActivity implements View.OnClickListener {

    private TitleBar titleBar;
    private ImageView imageView;
    private Button btn_pat, btn_picture;
    private TextView remarkTv;

    //    private Bitmap bitmap;
    private String filepath;
    private Intent intent;
    private Uri uri;
    private File imgFile;
    private int defaultPic;

    private int[] defaultImage = new int[]{R.drawable.sfzzm, R.drawable.sfzfm, R.drawable.scsfz, R.drawable.yhkzm, R.drawable.yyzz, R.drawable.mtz, R.drawable.dnz, R.drawable.htyz};
    private int[] paiXu = new int[]{9, 10, 8, 11, 12, 13, 14, 16};//照片展示的排序规则,//@[@"9",@"10",@"8",@"11",@"12",@"13",@"14"];

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_image;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("完成");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        imageView = getViewById(R.id.iv_image);
        btn_pat = getViewById(R.id.btn_pat);
        btn_picture = getViewById(R.id.btn_picture);
        remarkTv = getViewById(R.id.tv_photo_remark);

        width = 300;
        height = 300;

//        imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
//            public boolean onPreDraw() {
//                height = imageView.getMeasuredHeight();
//                width = imageView.getMeasuredWidth();
//                return true;
//            }
//        });
    }

    @Override
    protected void eventOnClick() {
        //控制拍照和相册
        intent = getIntent();
        if ("1".equals(intent.getStringExtra("photo"))) {
            btn_picture.setVisibility(View.GONE);
        }
        titleBar.setTiteTextView(intent.getStringExtra("tipName"));
        remarkTv.setText(intent.getStringExtra("remark"));
        defaultPic = Integer.parseInt(intent.getStringExtra(Constant.ITEM_ID));

        filepath = intent.getStringExtra("filePath");
        if (filepath != null) {
            imageView.setImageBitmap(BitmapFactory.decodeFile(filepath));
        }else {//如果没有拍照，则显示示例图
            imageView.setImageResource(getDefaultPic(defaultPic));
        }
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("uri", uri);
                intent.putExtra("filepath", filepath);
                intent.putExtra("imgFile", imgFile);
                setResult(RESULT_OK, intent);
                ChooseImageActivity.this.finish();
            }
        });
        btn_pat.setOnClickListener(this);
        btn_picture.setOnClickListener(this);
    }

    private int getDefaultPic(int merid) {
        int size = paiXu.length;
        for (int i = 0; i < size; i++) {
            if (merid == paiXu[i]) {
                return defaultImage[i];
            }
        }
        return defaultImage[0];//防止异常，如果没匹配到就暂时用第一张
    }

    @Override
    protected String getPhotoPath() {
        return "eeepay";
    }

    @Override
    protected void selectPhotoPathResult(File imgFile, Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
//        this.imgFile = imgFile;
//        imageView.setImageBitmap(BitmapUtils.createBitmap(this, uri, imgFile));
        Log.d("filePath", "相册读取路径：" + filepath);
        Intent intent = new Intent();
        intent.putExtra(Constant.FILE_PATH, imgFile.getPath());
        intent.putExtra(Constant.IMG_FILE, imgFile);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pat:
//                setNowPath("sss");
                setNowPath(intent.getStringExtra(Constant.ITEM_ID));
                selectCamera();
                break;
            case R.id.btn_picture:
//                setNowPath("sss");
                setNowPath(intent.getStringExtra(Constant.ITEM_ID));
                selectAlbum();
                break;
        }
    }
}
